using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCWeb.Models;
using MVCWeb.Models.ViewData;

namespace MVCWeb.Controllers
{
    public class VendorCommunityAdsController : BaseController
    {
        //
        // GET: /VendorCommunityAds/
        [Authorize(Roles = "Administrators")]
        public ActionResult Index()
        {
            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            int communityId = Convert.ToInt32(Session["CommunityId"]);
            return View(this.dc.VendorCommunityAds.Where(c => c.CommunityId == communityId).ToList<VendorCommunityAds>());
        }

        //
        // GET: /VendorCommunityAds/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Setup()
        {
            VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
            data.VendorCommunityAds = new VendorCommunityAds();
            data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
            data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
            //ViewData["CommunityList"] = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
            //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
            return View(data);
        }

        //
        // POST: /VendorCommunityAds/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Setup(VendorCommunityAds vendorCommunityAdsToCreate, FormCollection collection)
        {
            try
            {
                string[] list = collection["CommunityList"].Split(',');
                // TODO: Add insert logic here
                foreach (string cid in list)
                {
                    VendorCommunityAds itemToCreate = new VendorCommunityAds();
                    itemToCreate.CommunityId = Convert.ToInt32(cid);
                    itemToCreate.VendorId = vendorCommunityAdsToCreate.VendorId;
                    itemToCreate.AdsId = vendorCommunityAdsToCreate.AdsId;
                    itemToCreate.Active = vendorCommunityAdsToCreate.Active;
                    itemToCreate.Started = vendorCommunityAdsToCreate.Started;
                    itemToCreate.Ended = vendorCommunityAdsToCreate.Ended;
                    this.dc.VendorCommunityAds.InsertOnSubmit(itemToCreate);
                }
                this.dc.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "There are any setups is failed because it's exists");
                VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
                data.VendorCommunityAds = new VendorCommunityAds();
                data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
                data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
                //ViewData["CommunityList"] = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                return View(data);
            }
        }


        //
        // GET: /VendorCommunityAds/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
            data.VendorCommunityAds = new VendorCommunityAds();
            data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
            data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
            //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
            return View(data);
        } 

        //
        // POST: /VendorCommunityAds/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Create(VendorCommunityAds vendorCommunityAdsToCreate, FormCollection collection)
        {
            try
            {
                if (Session["CommunityId"] == null)
                {
                    return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
                }


                // TODO: Add insert logic here
                try
                {
                    VendorCommunityAds item = this.dc.VendorCommunityAds.Single(x => (x.VendorId == vendorCommunityAdsToCreate.VendorId) && (x.CommunityId == vendorCommunityAdsToCreate.CommunityId) && (x.AdsId == vendorCommunityAdsToCreate.AdsId));
                    if (item != null)
                    {
                        ModelState.AddModelError("", "This setup is failed because it's exists");
                        VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
                        data.VendorCommunityAds = new VendorCommunityAds();
                        data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
                        data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
                        //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                        return View(data);
                        
                        //this.dc.VendorCommunityAds.DeleteOnSubmit(item);
                        //this.dc.SubmitChanges();

                    }
                }
                catch { }
                
                vendorCommunityAdsToCreate.CommunityId = Convert.ToInt32(Session["CommunityId"]);
                this.dc.VendorCommunityAds.InsertOnSubmit(vendorCommunityAdsToCreate);
                this.dc.SubmitChanges();
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect(Request.RawUrl);
                }
            }
            catch
            {
                VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
                data.VendorCommunityAds = new VendorCommunityAds();
                data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
                data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
                //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                return View(data);
            }
        }

        //
        // GET: /VendorCommunityAds/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int vendorId, int communityId, int adsId)
        {
            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
            data.VendorCommunityAds = this.dc.VendorCommunityAds.Single(x => (x.VendorId == vendorId) && (x.CommunityId == communityId) && (x.AdsId == adsId));
            data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
            data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
            //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
            return View(data);
        }

        //
        // POST: /VendorCommunityAds/Edit/5

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Edit(int vendorId, int communityId, int adsId, VendorCommunityAds itemToEdit, FormCollection collection)
        {
            try
            {
                if (Session["CommunityId"] == null)
                {
                    return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
                }
                communityId = Convert.ToInt32(Session["CommunityId"]);
                // TODO: Add update logic here
                int vId = Convert.ToInt32(collection["vId"]);
                int aId = Convert.ToInt32(collection["aId"]);

                itemToEdit = this.dc.VendorCommunityAds.Single(x => (x.VendorId == vId) && (x.CommunityId == communityId) && (x.AdsId == aId));
                this.dc.VendorCommunityAds.DeleteOnSubmit(itemToEdit);
                this.dc.SubmitChanges();

                try
                {
                    VendorCommunityAds item = new VendorCommunityAds();
                    UpdateModel<VendorCommunityAds>(item);
                    this.dc.VendorCommunityAds.InsertOnSubmit(item);
                    this.dc.SubmitChanges();
                }
                catch 
                {
                    ModelState.AddModelError("", "This setup is failed because it's exists");
                    VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
                    data.VendorCommunityAds = this.dc.VendorCommunityAds.Single(x => (x.VendorId == vendorId) && (x.CommunityId == communityId) && (x.AdsId == adsId));
                    data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
                    data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
                    //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                    return View(data);
                }
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect(Request.RawUrl);
                }

                //if (this.dc.VendorCommunityAds.Where(x => (x.VendorId == itemToCreate.VendorId) && (x.CommunityId == itemToCreate.CommunityId) && (x.AdsId == itemToCreate.AdsId)) != null)
                //{
                //    ModelState.AddModelError("", "This setup is failed because it's exists");
                //}

                //if (ModelState.IsValid)
                //{
                //    this.dc.VendorCommunityAds.InsertOnSubmit(itemToCreate);
                //    this.dc.SubmitChanges();
                //    return RedirectToAction("Index");
                //}
                //else
                //{
                //    VendorCommunityAdsNewViewData data = new VendorCommunityAdsNewViewData();
                //    data.VendorCommunityAds = this.dc.VendorCommunityAds.Single(x => (x.VendorId == vendorId) && (x.CommunityId == communityId) && (x.AdsId == adsId));
                //    data.VendorList = this.dc.Vendors.OrderBy(v => v.VendorName).ToList<Vendor>();
                //    data.AdsList = this.dc.Ads.OrderBy(a => a.Type).ToList<Ads>();
                //    //data.CommunityList = this.dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                //    return View(data);
                //}
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Delete(int vendorId, int communityId, int adsId)
        {
            try
            {
                // TODO: Add update logic here
                VendorCommunityAds itemToDelete = this.dc.VendorCommunityAds.Single(x => (x.VendorId == vendorId) && (x.CommunityId == communityId) && (x.AdsId == adsId));
                this.dc.VendorCommunityAds.DeleteOnSubmit(itemToDelete);
                this.dc.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
